/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inter;

import Logic.Conectividad;
import Logic.MessageBox;
import Logic.This;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ronaldo
 */
public class CtrCliente extends javax.swing.JInternalFrame {

    private int Modus = 0;

    /**
     * Creates new form CtrCliente
     */
    public CtrCliente() {
        initComponents();
        Clean();
    }

    private void Clean() {
        txtCodigo.setText(This.AlfaNum(10));
        txtNombre.setText("");
        txtTelefono.setText("");
        txtCorreo.setText("");
        txtUbicacion.setText("");
        Load();
    }

    private void Load() {
        try {
            javax.swing.table.DefaultTableModel Modelo = (javax.swing.table.DefaultTableModel) jTable1.getModel();
            Modelo.setRowCount(0);

            String Sql = "SELECT * FROM CLIENTES";
            java.sql.Statement Cons = Conectividad.Conector.createStatement();
            java.sql.ResultSet Result = Cons.executeQuery(Sql);

            while (Result.next()) {
                java.util.Vector v = new java.util.Vector();
                v.add(Result.getString(1));
                v.add(Result.getString(2));
                v.add(Result.getString(3));
                v.add(Result.getString(4));
                v.add(Result.getString(5));
                Modelo.addRow(v);
            }
            jTable1.setModel(Modelo);
        } catch (SQLException ex) {
            Logger.getLogger(CtrCliente.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void Save() {
        try {
            String Codigo, Nombre, Telefono, Correo, Ubicacion;
            Codigo = txtCodigo.getText();
            Nombre = txtNombre.getText();
            Telefono = txtTelefono.getText();
            Correo = txtCorreo.getText();
            Ubicacion = txtUbicacion.getText();

            String Sql = "";
            if (Modus == 0) {
                Sql = "INSERT INTO CLIENTES VALUES('" + Codigo + "','" + Nombre + "','" + Telefono + "','" + Correo + "','" + Ubicacion + "')";
            } else if (Modus == 1) {
                Sql = "UPDATE CLIENTES SET NOMBRE='" + Nombre + "',TELEFONO='" + Telefono + "',CORREO='" + Correo + "',UBICACION='" + Ubicacion + "' WHERE COD_CLIE='" + Codigo + "'";
            }
            java.sql.Statement Cons = Conectividad.Conector.createStatement();
            Cons.executeUpdate(Sql);
            MessageBox.show(this, "Exito!", "Registrado exitosamente.!", MessageBox.Informacion);
            Clean();
        } catch (java.sql.SQLException ex) {
            MessageBox.show(this, "Error presentado", "Se ha presentado un error\n" + ex.getMessage(), MessageBox.Error);
        }
    }

    private void Selected() {
        txtCodigo.setText(ValT(0));
        txtNombre.setText(ValT(1));
        txtTelefono.setText(ValT(2));
        txtCorreo.setText(ValT(3));
        txtUbicacion.setText(ValT(4));
        Modus = 1;
    }

    private String ValT(int C) {
        return jTable1.getValueAt(jTable1.getSelectedRow(), C).toString();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtTelefono = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        txtCorreo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtUbicacion = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        lblSave = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Control de Clientes");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Control de Clientes", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Century Schoolbook L", 1, 10))); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos del Cliente", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century Schoolbook L", 1, 10))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Century Schoolbook L", 0, 10)); // NOI18N
        jLabel1.setText("Codigo");

        txtCodigo.setEditable(false);
        txtCodigo.setFont(new java.awt.Font("Century Schoolbook L", 0, 11)); // NOI18N
        txtCodigo.setPreferredSize(new java.awt.Dimension(10, 20));

        jLabel2.setFont(new java.awt.Font("Century Schoolbook L", 0, 10)); // NOI18N
        jLabel2.setText("Nombre");

        txtNombre.setFont(new java.awt.Font("Century Schoolbook L", 0, 11)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Century Schoolbook L", 0, 10)); // NOI18N
        jLabel3.setText("Telefono");

        try {
            txtTelefono.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        txtTelefono.setFont(new java.awt.Font("Century Schoolbook L", 0, 11)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Century Schoolbook L", 0, 10)); // NOI18N
        jLabel4.setText("Correo");

        txtCorreo.setFont(new java.awt.Font("Century Schoolbook L", 0, 11)); // NOI18N

        jLabel5.setFont(new java.awt.Font("Century Schoolbook L", 0, 10)); // NOI18N
        jLabel5.setText("Ubicacion");

        txtUbicacion.setFont(new java.awt.Font("Century Schoolbook L", 0, 11)); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCodigo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtNombre))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(txtCorreo))
                    .addComponent(txtUbicacion, javax.swing.GroupLayout.PREFERRED_SIZE, 485, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(txtCorreo, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtUbicacion, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/edit-clear.png"))); // NOI18N
        jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel8MouseClicked(evt);
            }
        });

        lblSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/sfile.png"))); // NOI18N
        lblSave.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        lblSave.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblSaveMouseClick1(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblSaveMouseExit1(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblSaveMouseEnter1(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Contenido de la información", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Century Schoolbook L", 1, 10))); // NOI18N
        jPanel3.setLayout(new java.awt.BorderLayout());

        jTable1.setFont(new java.awt.Font("Century Schoolbook L", 0, 12)); // NOI18N
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Telefono", "Correo", "Ubicacion"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblSave)
                .addContainerGap())
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblSave)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jLabel8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel8MouseClicked
        // TODO add your handling code here:
        Clean();
    }//GEN-LAST:event_jLabel8MouseClicked

    private void lblSaveMouseClick1(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblSaveMouseClick1
        // TODO add your handling code here:
        Save();
    }//GEN-LAST:event_lblSaveMouseClick1

    private void lblSaveMouseExit1(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblSaveMouseExit1
        // TODO add your handling code here:
        lblSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/sfile.png")));
    }//GEN-LAST:event_lblSaveMouseExit1

    private void lblSaveMouseEnter1(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblSaveMouseEnter1
        // TODO add your handling code here:
        lblSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/sfile2.png")));
    }//GEN-LAST:event_lblSaveMouseEnter1

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            Selected();
        }
    }//GEN-LAST:event_jTable1MouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblSave;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtCorreo;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JFormattedTextField txtTelefono;
    private javax.swing.JTextField txtUbicacion;
    // End of variables declaration//GEN-END:variables
}
